part of '../widgets.dart';

/// [VanField]属性常量
const kVanFieldLabelWidth = VanCssKey._raw('--van-field-label-width');
const kVanFieldLabelColor = VanCssKey._raw('--van-field-label-color');
const kVanFieldLabelMarginRight = VanCssKey._raw('--van-field-label-margin-right');
const kVanFieldInputTextColor = VanCssKey._raw('--van-field-input-text-color');
const kVanFieldInputErrorTextColor = VanCssKey._raw('--van-field-input-error-text-color');
const kVanFieldInputDisabledTextColor = VanCssKey._raw('--van-field-input-disabled-text-color');
const kVanFieldPlaceholderTextColor = VanCssKey._raw('--van-field-placeholder-text-color');
const kVanFieldIconSize = VanCssKey._raw('--van-field-icon-size');
const kVanFieldClearIconSize = VanCssKey._raw('--van-field-clear-icon-size');
const kVanFieldClearIconColor = VanCssKey._raw('--van-field-clear-icon-color');
const kVanFieldRightIconColor = VanCssKey._raw('--van-field-right-icon-color');
const kVanFieldErrorMessageColor = VanCssKey._raw('--van-field-error-message-color');
const kVanFieldErrorMessageFontSize = VanCssKey._raw('--van-field-error-message-font-size');
const kVanFieldTextAreaMinHeight = VanCssKey._raw('--van-field-text-area-min-height');
const kVanFieldWordLimitColor = VanCssKey._raw('--van-field-word-limit-color');
const kVanFieldWordLimitFontSize = VanCssKey._raw('--van-field-word-limit-font-size');
const kVanFieldWordLimitLineHeight = VanCssKey._raw('--van-field-word-limit-line-height');
const kVanFieldDisabledTextColor = VanCssKey._raw('--van-field-disabled-text-color');
const kVanFieldRequiredMarkColor = VanCssKey._raw('--van-field-required-mark-color');


/// [VanField]主题类
@immutable
class VanFieldTheme extends VanProvider<VanFieldThemeData> with VanProvide {
  const VanFieldTheme({super.key, required super.child, required super.provide});

  static VanFieldThemeData of(BuildContext context) => VanProvider.of<VanFieldTheme>(context)?.provide ?? VanTheme.of(context).fieldTheme;
  static const _prefix = "--van-field-";
  static bool support(VanCssKey key) => key.key.startsWith(_prefix);

  static dynamic get<T>(BuildContext context, {required VanCssKey key, T? defaultValue}) {
    final VanFieldThemeData fieldTheme = VanFieldTheme.of(context);
    if (kVanFieldLabelWidth == key) return fieldTheme.labelWidth.get(context, key);
    if (kVanFieldLabelColor == key) return fieldTheme.labelColor.get(context, key);
    if (kVanFieldLabelMarginRight == key) return fieldTheme.labelMarginRight.get(context, key);
    if (kVanFieldInputTextColor == key) return fieldTheme.inputTextColor.get(context, key);
    if (kVanFieldInputErrorTextColor == key) return fieldTheme.inputErrorTextColor.get(context, key);
    if (kVanFieldInputDisabledTextColor == key) return fieldTheme.inputDisabledTextColor.get(context, key);
    if (kVanFieldPlaceholderTextColor == key) return fieldTheme.placeholderTextColor.get(context, key);
    if (kVanFieldIconSize == key) return fieldTheme.iconSize.get(context, key);
    if (kVanFieldClearIconSize == key) return fieldTheme.clearIconSize.get(context, key);
    if (kVanFieldClearIconColor == key) return fieldTheme.clearIconColor.get(context, key);
    if (kVanFieldRightIconColor == key) return fieldTheme.rightIconColor.get(context, key);
    if (kVanFieldErrorMessageColor == key) return fieldTheme.errorMessageColor.get(context, key);
    if (kVanFieldErrorMessageFontSize == key) return fieldTheme.errorMessageFontSize.get(context, key);
    if (kVanFieldTextAreaMinHeight == key) return fieldTheme.textAreaMinHeight.get(context, key);
    if (kVanFieldWordLimitColor == key) return fieldTheme.wordLimitColor.get(context, key);
    if (kVanFieldWordLimitFontSize == key) return fieldTheme.wordLimitFontSize.get(context, key);
    if (kVanFieldWordLimitLineHeight == key) return fieldTheme.wordLimitLineHeight.get(context, key);
    if (kVanFieldDisabledTextColor == key) return fieldTheme.disabledTextColor.get(context, key);
    if (kVanFieldRequiredMarkColor == key) return fieldTheme.requiredMarkColor.get(context, key);
    return null;
  }
}

/// [VanField]主题数据类
@immutable
class VanFieldThemeData with VanProvide {
  final VanCss<double> labelWidth;
  final VanCss<Color> labelColor;
  final VanCss<EdgeInsets> labelMarginRight;
  final VanCss<Color> inputTextColor;
  final VanCss<Color> inputErrorTextColor;
  final VanCss<Color> inputDisabledTextColor;
  final VanCss<Color> placeholderTextColor;
  final VanCss<double> iconSize;
  final VanCss<double> clearIconSize;
  final VanCss<Color> clearIconColor;
  final VanCss<Color> rightIconColor;
  final VanCss<Color> errorMessageColor;
  final VanCss<double> errorMessageFontSize;
  final VanCss<double> textAreaMinHeight;
  final VanCss<Color> wordLimitColor;
  final VanCss<double> wordLimitFontSize;
  final VanCss<double> wordLimitLineHeight;
  final VanCss<Color> disabledTextColor;
  final VanCss<Color> requiredMarkColor;

  /// 构造器
  const VanFieldThemeData.raw({
    required this.labelWidth,
    required this.labelColor,
    required this.labelMarginRight,
    required this.inputTextColor,
    required this.inputErrorTextColor,
    required this.inputDisabledTextColor,
    required this.placeholderTextColor,
    required this.iconSize,
    required this.clearIconSize,
    required this.clearIconColor,
    required this.rightIconColor,
    required this.errorMessageColor,
    required this.errorMessageFontSize,
    required this.textAreaMinHeight,
    required this.wordLimitColor,
    required this.wordLimitFontSize,
    required this.wordLimitLineHeight,
    required this.disabledTextColor,
    required this.requiredMarkColor,
  });

  /// 拷贝
  VanFieldThemeData copyFrom({
    VanCss<double>? labelWidth,
    VanCss<Color>? labelColor,
    VanCss<EdgeInsets>? labelMarginRight,
    VanCss<Color>? inputTextColor,
    VanCss<Color>? inputErrorTextColor,
    VanCss<Color>? inputDisabledTextColor,
    VanCss<Color>? placeholderTextColor,
    VanCss<double>? iconSize,
    VanCss<double>? clearIconSize,
    VanCss<Color>? clearIconColor,
    VanCss<Color>? rightIconColor,
    VanCss<Color>? errorMessageColor,
    VanCss<double>? errorMessageFontSize,
    VanCss<double>? textAreaMinHeight,
    VanCss<Color>? wordLimitColor,
    VanCss<double>? wordLimitFontSize,
    VanCss<double>? wordLimitLineHeight,
    VanCss<Color>? disabledTextColor,
    VanCss<Color>? requiredMarkColor,
  }) {
    return VanFieldThemeData.raw(
      labelWidth: labelWidth ?? this.labelWidth,
      labelColor: labelColor ?? this.labelColor,
      labelMarginRight: labelMarginRight ?? this.labelMarginRight,
      inputTextColor: inputTextColor ?? this.inputTextColor,
      inputErrorTextColor: inputErrorTextColor ?? this.inputErrorTextColor,
      inputDisabledTextColor: inputDisabledTextColor ?? this.inputDisabledTextColor,
      placeholderTextColor: placeholderTextColor ?? this.placeholderTextColor,
      iconSize: iconSize ?? this.iconSize,
      clearIconSize: clearIconSize ?? this.clearIconSize,
      clearIconColor: clearIconColor ?? this.clearIconColor,
      rightIconColor: rightIconColor ?? this.rightIconColor,
      errorMessageColor: errorMessageColor ?? this.errorMessageColor,
      errorMessageFontSize: errorMessageFontSize ?? this.errorMessageFontSize,
      textAreaMinHeight: textAreaMinHeight ?? this.textAreaMinHeight,
      wordLimitColor: wordLimitColor ?? this.wordLimitColor,
      wordLimitFontSize: wordLimitFontSize ?? this.wordLimitFontSize,
      wordLimitLineHeight: wordLimitLineHeight ?? this.wordLimitLineHeight,
      disabledTextColor: disabledTextColor ?? this.disabledTextColor,
      requiredMarkColor: requiredMarkColor ?? this.requiredMarkColor,
    );
  }

  /// 反序列化
  static VanFieldThemeData fromJson(Map<String, dynamic> json) {
    return VanFieldThemeData.raw(
      labelWidth: VanCss(json[kVanFieldLabelWidth.key]),
      labelColor: VanCss(json[kVanFieldLabelColor.key]),
      labelMarginRight: VanCss(json[kVanFieldLabelMarginRight.key]),
      inputTextColor: VanCss(json[kVanFieldInputTextColor.key]),
      inputErrorTextColor: VanCss(json[kVanFieldInputErrorTextColor.key]),
      inputDisabledTextColor: VanCss(json[kVanFieldInputDisabledTextColor.key]),
      placeholderTextColor: VanCss(json[kVanFieldPlaceholderTextColor.key]),
      iconSize: VanCss(json[kVanFieldIconSize.key]),
      clearIconSize: VanCss(json[kVanFieldClearIconSize.key]),
      clearIconColor: VanCss(json[kVanFieldClearIconColor.key]),
      rightIconColor: VanCss(json[kVanFieldRightIconColor.key]),
      errorMessageColor: VanCss(json[kVanFieldErrorMessageColor.key]),
      errorMessageFontSize: VanCss(json[kVanFieldErrorMessageFontSize.key]),
      textAreaMinHeight: VanCss(json[kVanFieldTextAreaMinHeight.key]),
      wordLimitColor: VanCss(json[kVanFieldWordLimitColor.key]),
      wordLimitFontSize: VanCss(json[kVanFieldWordLimitFontSize.key]),
      wordLimitLineHeight: VanCss(json[kVanFieldWordLimitLineHeight.key]),
      disabledTextColor: VanCss(json[kVanFieldDisabledTextColor.key]),
      requiredMarkColor: VanCss(json[kVanFieldRequiredMarkColor.key]),
    );
  }

}

/// [VanField]样式
@immutable
class VanFieldStyle with VanProvide {
  static of(BuildContext context) => VanProvider.of<VanFieldStyle>(context);

  final double? labelWidth;
  final Color? labelColor;
  final double? labelMarginRight;
  final Color? inputTextColor;
  final Color? inputErrorTextColor;
  final Color? placeholderTextColor;
  final double? iconSize;
  final double? clearIconSize;
  final Color? clearIconColor;
  final Color? rightIconColor;
  final Color? errorMessageColor;
  final double? errorMessageFontSize;
  final double? textAreaMinHeight;
  final Color? wordLimitColor;
  final double? wordLimitFontSize;
  final double? wordLimitLineHeight;
  final Color? textColor;
  final Color? requiredMarkColor;

  VanFieldStyle({
    this.labelWidth,
    this.labelColor,
    this.labelMarginRight,
    this.inputTextColor,
    this.inputErrorTextColor,
    this.placeholderTextColor,
    this.iconSize,
    this.clearIconSize,
    this.clearIconColor,
    this.rightIconColor,
    this.errorMessageColor,
    this.errorMessageFontSize,
    this.textAreaMinHeight,
    this.wordLimitColor,
    this.wordLimitFontSize,
    this.wordLimitLineHeight,
    this.textColor,
    this.requiredMarkColor,
  });

  bool get isNonNull {
    if (labelWidth == null) return false;
    if (labelColor == null) return false;
    if (labelMarginRight == null) return false;
    if (inputTextColor == null) return false;
    if (inputErrorTextColor == null) return false;
    if (inputTextColor == null) return false;
    if (placeholderTextColor == null) return false;
    if (iconSize == null) return false;
    if (clearIconSize == null) return false;
    if (clearIconColor == null) return false;
    if (rightIconColor == null) return false;
    if (errorMessageColor == null) return false;
    if (errorMessageFontSize == null) return false;
    if (textAreaMinHeight == null) return false;
    if (wordLimitColor == null) return false;
    if (wordLimitFontSize == null) return false;
    if (wordLimitLineHeight == null) return false;
    if (textColor == null) return false;
    if (requiredMarkColor == null) return false;
    return true;
  }

  VanFieldStyle copyFrom({
    double? labelWidth,
    Color? labelColor,
    double? labelMarginRight,
    Color? inputTextColor,
    Color? inputErrorTextColor,
    Color? placeholderTextColor,
    double? iconSize,
    double? clearIconSize,
    Color? clearIconColor,
    Color? rightIconColor,
    Color? errorMessageColor,
    double? errorMessageFontSize,
    double? textAreaMinHeight,
    Color? wordLimitColor,
    double? wordLimitFontSize,
    double? wordLimitLineHeight,
    Color? textColor,
    Color? requiredMarkColor,
  }) {
    return VanFieldStyle(
      labelWidth: labelWidth ?? this.labelWidth,
      labelColor: labelColor ?? this.labelColor,
      labelMarginRight: labelMarginRight ?? this.labelMarginRight,
      inputTextColor: inputTextColor ?? this.inputTextColor,
      inputErrorTextColor: inputErrorTextColor ?? this.inputErrorTextColor,
      placeholderTextColor: placeholderTextColor ?? this.placeholderTextColor,
      iconSize: iconSize ?? this.iconSize,
      clearIconSize: clearIconSize ?? this.clearIconSize,
      clearIconColor: clearIconColor ?? this.clearIconColor,
      rightIconColor: rightIconColor ?? this.rightIconColor,
      errorMessageColor: errorMessageColor ?? this.errorMessageColor,
      errorMessageFontSize: errorMessageFontSize ?? this.errorMessageFontSize,
      textAreaMinHeight: textAreaMinHeight ?? this.textAreaMinHeight,
      wordLimitColor: wordLimitColor ?? this.wordLimitColor,
      wordLimitFontSize: wordLimitFontSize ?? this.wordLimitFontSize,
      wordLimitLineHeight: wordLimitLineHeight ?? this.wordLimitLineHeight,
      textColor: textColor ?? this.textColor,
      requiredMarkColor: requiredMarkColor ?? this.requiredMarkColor,
    );
  }
}